/**
  ******************************************************************************
  * @file    sym32l010_startkit_board.c
  * @author  AE Team
  * @version 1.0.3
  * @date    2024-05-28
  * @brief   sym32l010 startkit board support package.
  *
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2024 SIYIMicro.
  * All rights reserved.</center></h2>
  *
  *
  ******************************************************************************
  */


/******************************************************************************/
/* Include files                                                              */
/******************************************************************************/
#include "sym32l010_startkit_board.h"


/*******************************************************************************
 * @brief  用于支持Printf打印的函数，
 * @retval None
 * @note   需要使用microlib库
 */
#if (__ARMCC_VERSION > 6000000)
    __asm (".global __use_no_semihosting\n\t");
    void _sys_exit(int x)
    {
        x = x;
    }
    /* __use_no_semihosting was requested, but _ttywrch was */
    void _ttywrch(int ch)
    {
        ch = ch;
    }
    FILE __stdout;
#else
    #ifdef __CC_ARM
    #pragma import(__use_no_semihosting)
    struct __FILE
    {
        int handle;
    };
    FILE __stdout;
    void _sys_exit(int x)
    {
        x = x;
    }
    /* __use_no_semihosting was requested, but _ttywrch was */
    void _ttywrch(int ch)
    {
        ch = ch;
    }
    #endif
#endif

#if defined (__GNUC__) && !defined (__clang__)
    #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
    #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif


/*******************************************************************************
 * @brief  重定向C库中Printf函数到UART.
 * @retval None
 * @note   None
 */
PUTCHAR_PROTOTYPE
{
    while ((BSP_UART->ISR & UARTx_ISR_TXE_Msk) != UARTx_ISR_TXE_Msk);
    BSP_UART->TDR = ch;
    while ((BSP_UART->ISR & UARTx_ISR_TXBUSY_Msk) == UARTx_ISR_TXBUSY_Msk);
    return ch;
}


/*******************************************************************************
  * @brief  板载UART打印初始化
  * @param  pclkFreq: pclk freq
  * @param  baudRate: uart baudrate
  * @retval none
  * @note   请在J13上安装跳线帽
  */
void Bsp_Uart_Init(uint32_t pclkFreq, uint32_t baudRate)
{
    /* configure the uart txd pin */
    BSP_UART_TXD_PORT_PERIPH_CLK_ENABLE();
    BSP_UART_TXD_AF();
    BSP_UART_TXD_PORT->ANALOG &= ~(BSP_UART_TXD_PIN);

    /* configure the uart rxd pin */
    BSP_UART_RXD_PORT_PERIPH_CLK_ENABLE();
    BSP_UART_RXD_AF();
    BSP_UART_RXD_PORT->ANALOG &= ~(BSP_UART_RXD_PIN);
    
    /* configure uart */
    BSP_UART_PERIPH_CLK_ENABLE();
    BSP_UART_PERIPH_RST_ENABLE();
    BSP_UART_PERIPH_RST_DISABLE();
    
    uint32_t BRR = (pclkFreq + (baudRate>>1UL)) / baudRate;
    
    BSP_UART->BRRI = BRR >> 4UL;
    BSP_UART->BRRF = BRR;
    BSP_UART->CR1  = UARTx_CR1_TXEN_Msk | UARTx_CR1_RXEN_Msk;
}


/*******************************************************************************
  * @brief  板载按键初始化
  * @param  None
  * @retval None
  * @note   请短接短路点J25
  */
void Bsp_Key_Init(void)
{
    /* enable the key clock */
    BSP_KEY_PERIPH_CLK_ENABLE();

    /* configure key pin as input with pull-up */
    BSP_KEY_PORT->DIR    |=  (BSP_KEY_PIN);    // Input
    BSP_KEY_PORT->PUR    |=  (BSP_KEY_PIN);    // Pull Up
    BSP_KEY_PORT->ANALOG &= ~(BSP_KEY_PIN);    // Digital
    
//    BSP_KEY_PORT->FALLIE |= (BSP_KEY_PIN);     // Enable Fall Interrupt
//    NVIC_ClearPendingIRQ(BSP_KEY_IRQn);
//    NVIC_EnableIRQ(BSP_KEY_IRQn);
}


/*******************************************************************************
  * @brief  读取板载按键的状态
  * @param  none
  * @retval 返回板载按键的电平状态
  *         0: 按键按下， 1：按键未按下
  */
__INLINE uint8_t Bsp_Read_Key(void)
{
    return ((BSP_KEY_GETVALUE() == 0) ? 0 : 1);
}


/*******************************************************************************
  * @brief  板载LED初始化
  * @param  None
  * @retval None
  * @note   请短接短路点J28
  */
void Bsp_Led_Init(void)
{
    /* enable the led clock */
    BSP_LED_PERIPH_CLK_ENABLE();

    /* configure the led gpio */
    BSP_LED_SETHIGH();                            // Default output value

    BSP_LED_PORT->OPENDRAIN &= ~(BSP_LED_PIN);    // Push-Pull
    BSP_LED_PORT->DIR       &= ~(BSP_LED_PIN);    // Output
    BSP_LED_PORT->ANALOG    &= ~(BSP_LED_PIN);    // Digital
}


/*******************************************************************************
  * @brief  点亮板载LED
  * @param  None
  * @retval None
  */
__INLINE void Bsp_Led_On(void)
{
    BSP_LED_SETLOW();
}


/*******************************************************************************
  * @brief  熄灭板载LED
  * @param  None
  * @retval None
  */
__INLINE void Bsp_Led_Off(void)
{
    BSP_LED_SETHIGH();
}


/*******************************************************************************
  * @brief  翻转板载LED
  * @param  None
  * @retval None
  */
__INLINE void Bsp_Led_Tog(void)
{
    BSP_LED_TOG();
}


/*******************************************************************************
  * @brief  板载电位器的端口初始化
  * @param  None
  * @retval None
  * @note   1.该函数将板载电位器的IO端口初始化为模拟状态
  *         2.请短接短路点J23、J24
  */
void Bsp_RheostatPort_Init(void)
{
    BSP_RHEOSTAT_PERIPH_CLK_ENABLE();
    BSP_RHEOSTAT_PORT->ANALOG |= (BSP_RHEOSTAT_PIN);
}


/*******************************************************************************
  * @brief  板载红外发射管的端口初始化
  * @param  None
  * @retval None
  * @note   1.将板载红外发射管的IO端口初始化数字输出复用
  *         2.请短接短路点J26、J27
  */
void Bsp_IrPort_Init(void)
{
    BSP_IR_PERIPH_CLK_ENABLE();

    REGBITS_MODIFY(BSP_IR_PORT->AFRL, GPIOx_AFRL_AFR3_Msk, 0x5 << GPIOx_AFRL_AFR3_Pos);

    BSP_IR_PORT->BRR        = BSP_IR_PIN;       // Set low
    BSP_IR_PORT->OPENDRAIN &= ~(BSP_IR_PIN);    // Push-Pull
    BSP_IR_PORT->DIR       &= ~(BSP_IR_PIN);    // Output
    BSP_IR_PORT->ANALOG    &= ~(BSP_IR_PIN);    // Digital
}

/*******************************************************************************
  * @brief  板载硬件接口初始化
  * @param  None
  * @retval None
  */
 void Bsp_Init(uint32_t ClkFreq)
 {
     Bsp_Led_Init();
     Bsp_Key_Init();
     Bsp_RheostatPort_Init();
     Bsp_IrPort_Init();
     
     Bsp_Uart_Init(ClkFreq, 115200);
 }

/************************ (C) COPYRIGHT SIYIMicro *****END OF FILE*************/
